package io.github.talelin.latticy.controller.v1;


import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.model.TestsDO;
import io.github.talelin.latticy.service.TestsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Positive;
import java.io.IOException;
import java.util.List;

/**
* @author generator@TaleLin
* @since 2021-05-26
*/
@Controller
@RequestMapping("/v1/tests")
public class TestsController {
    @Autowired
    private TestsService testsService;

    @RequestMapping("ali")
    public void redirect(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.sendRedirect("https://www.baidu.com");
    }


    @GetMapping("")
    //@GroupRequired
    //@PermissionMeta(value = "查看所有测试", module = "趣味测试")
    public List<TestsDO> getTests() {
        List<TestsDO> tests = testsService.findAll();
        return tests;
    }

    @GetMapping("/{id}")
    public TestsDO getTest(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        TestsDO test = testsService.getById(id);
        if (test == null) {
            throw new NotFoundException(10022);
        }
        return test;
    }
}
